package com.gettec.lims.client.forms;

import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.gettec.lims.client.NlimsConstants;
import com.gettec.lims.client.bind.ComboBoxFieldBinding;
import com.gettec.lims.client.services.NnimsServiceAsync;
import com.gettec.lims.shared.model.RegSheet;
import com.gettec.lims.shared.model.SampleInfo;
import com.gettec.lims.shared.model.SysUser;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

public class SampleInfoForm extends Dialog
{
	protected Button reset;
	protected Button save;
	protected FormPanel form ;
	protected ButtonBar buttonBar;
	protected BeanModel sampleInfoModel;
	protected NnimsServiceAsync regSheetService;
	protected ListStore<BeanModel> store;
	private final TextField<String> name = new TextField<String>();  
	private final TextField<String> resource_id = new TextField<String>();  
	private final TextField<String> data_coverage_id = new TextField<String>();
	private final TextField<String> abstract_sample_id = new TextField<String>();   
	private final TextField<String> product_info_c_id = new TextField<String>();   
	private final TextField<String> product_category_id = new TextField<String>();
	private final TextField<String> product_type_id = new TextField<String>();  
	private final TextField<String> backup_storage_place_id = new TextField<String>();  
	private final TextField<String> sample_amount_value_unit_id = new TextField<String>();
	private final TextField<String> provider_info_id = new TextField<String>();  
	private final TextField<String> test_type_id = new TextField<String>();   
	private final TextField volume_for_test = new TextField();  
	private final TextField volume_for_backup = new TextField();
	 ComboBox<BeanModel>  order_serial_no = new ComboBox<BeanModel> ();  
	//private final TextField order_serial_no = new TextField(); 
	private final TextField<String> claimed_brand = new TextField<String>();  
	private final TextField<String> level = new TextField<String>();
	private final TextField<String> type = new TextField<String>();  
	private final TextField<String> format = new TextField<String>();  
	private final TextField<String> regularity = new TextField<String>();
	private final DateField production_date = new DateField();
	
	   
	private final TextField<String> batch_serial_no = new TextField<String>();
	private final TextField<String> serial = new TextField<String>();
	private final TextField<String> status = new TextField<String>();
	private final TextField<String> price = new TextField<String>();
	private final TextField<String> inventory_volume = new TextField<String>();
	private final TextField<String> purchase_volume = new TextField<String>();
	private final TextField<String> air_temperature = new TextField<String>();
	private final TextField<String> humidity = new TextField<String>();
	private final TextField<String> notes = new TextField<String>();
	@Override
	protected void onRender(Element target, int index) {
		// TODO Auto-generated method stub
		super.onRender(target, index);
	}
public SampleInfoForm(){}
	public SampleInfoForm(final NnimsServiceAsync regSheetService, final ListStore<BeanModel> store,final SampleInfo sampleInfo)
	{
		setWidth(600);
		this.regSheetService=regSheetService;
		this.store=store;
		String[] zh_captions={"样品名称","图片","数据范围","抽象样品ID","小类","牌号","型号","备份样品封存地","样品数量单位","生产企业",
				"抽样类型","检测用样品数量","备份用样品数量","标称商标","等级","型号","规格","标称标准","生产日期" , "批次",
				"样品编号","样品状态","销售单价","库存量","进货量","气温","湿度","备注"};
		Field[]fields={name,resource_id,data_coverage_id,abstract_sample_id,product_info_c_id,product_category_id,product_type_id,
				backup_storage_place_id,sample_amount_value_unit_id,provider_info_id,test_type_id,volume_for_test,volume_for_backup,
				claimed_brand,level,type,format,regularity,production_date,batch_serial_no,serial,
				status,price,inventory_volume,purchase_volume,air_temperature,humidity,notes};
		String[]captions={												     
				"name","resource_id","data_coverage_id","abstract_sample_id","product_info_c_id",
				"product_category_id","product_type_id","backup_storage_place_id","sample_amount_value_unit_id",
				"provider_info_id","test_type_id","volume_for_test","volume_for_backup",
				"claimed_brand","level","type","format","regularity","production_date","batch_serial_no",
				"serial","status","price","inventory_volume","purchase_volume","air_temperature","humidity",
				"notes"};
		  
			FormData formData = new FormData("100%");   
			form = new FormPanel();   
			form.setFrame(true);   
		   
		    //panel.setHeading("FormPanel");   
		   form.setSize(600, -1);   
			form.setAutoHeight(true);
			form.setAutoWidth(true);
			form.setBorders(false);
			form.setHideLabels(true);
			form.setHeaderVisible(false);
			form.setLabelAlign(LabelAlign.TOP);   
			form.setButtonAlign(HorizontalAlignment.CENTER);   
		  
			LayoutContainer main = new LayoutContainer(new RowLayout()); 
			main.setHeight(600);
			main.setWidth(100);
		    LayoutContainer sub_main = new LayoutContainer(); 
		    sub_main.setBorders(true);
		    sub_main.setLayout(new ColumnLayout()); 
		    LayoutContainer top = new LayoutContainer(); 
		    top.setLayout(new FormLayout(LabelAlign.LEFT));
		    LayoutContainer left = new LayoutContainer();   
		    left.setStyleAttribute("paddingMiddle", "10px");   
		    FormLayout layout = new FormLayout();   
		    layout.setLabelAlign(LabelAlign.TOP);   
		    left.setLayout(layout);   
		    LayoutContainer mid = new LayoutContainer();   
		    mid.setStyleAttribute("paddingRight", "10px");   
		     layout = new FormLayout();   
		    layout.setLabelAlign(LabelAlign.TOP);   
		    mid.setLayout(layout);   
		    LayoutContainer right = new LayoutContainer();   
		    right.setStyleAttribute("paddingMiddle", "10px");   
		    layout = new FormLayout();   
		    layout.setLabelAlign(LabelAlign.TOP);   
		    right.setLayout(layout);   
		    final NnimsServiceAsync service=Registry.get(NlimsConstants.SERVICE);
		    RpcProxy<List<RegSheet>> proxy = new RpcProxy<List<RegSheet>>() {  
	            @Override  
	            protected void load(Object loadConfig,  
	                      AsyncCallback<List<RegSheet>> callback) {  
	            	service.getClientSerialList(callback);  
	             }  
	         };  
	         BeanModelReader reader = new BeanModelReader();  
	        ListLoader<ListLoadResult> loader = new BaseListLoader<ListLoadResult>(proxy, reader);
	        ListStore<BeanModel> suStore = new ListStore<BeanModel>(loader);  
	        loader.load();  
	        order_serial_no.setTriggerAction (TriggerAction.ALL);
	        order_serial_no.setEmptyText("登记单编号...");   
	        order_serial_no.setDisplayField ("client_number");  
		   // combobox.setValue(value)
	        order_serial_no.setLayoutData(new FitLayout());
	        order_serial_no.setValueField ("client_number");  
	        order_serial_no.setStore (suStore); 
		    
	        order_serial_no.setName("order_serial_no");
	        order_serial_no.setFieldLabel("委托登记号");
	        top.add(order_serial_no);
		    for( int i=0;i<fields.length;i++)
			{
		    	
				fields[i].setName(captions[i]);
				fields[i].setFieldLabel(zh_captions[i]);
		    	
				if(i%3==0)
					left.add(fields[i], formData);   
				else if(i%3==1)
					mid.add(fields[i], formData);   
				else
					right.add(fields[i], formData);   
			}
		    main.add(top,new RowData(1,.2));
		    sub_main.add(left, new ColumnData(.3));   
		    sub_main.add(mid, new ColumnData(.3));   
		    sub_main.add(right, new ColumnData(.3));   
		    main.add(sub_main,new RowData(1,.8));
			    form.add(main, new FormData("100%")); 
			    sampleInfoModel = BeanModelLookup.get().getFactory(SampleInfo.class).createModel(sampleInfo);
				final FormBinding formBinding = new FormBinding(form, true);
				formBinding.bind(sampleInfoModel);
				formBinding.addFieldBinding(new ComboBoxFieldBinding(order_serial_no));
				add(form);
				buttonBar = new ButtonBar();
				createButton() ;
				setButtons("");//这样可以隐藏不必要的ok按钮
				 add(buttonBar, new FlowData(10));
	}
	protected void createButton() {
		
		reset = new Button("重置");
		reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				//CustomerDialog.this.hide();
				form.reset();
			}

		});

		save = new Button("保存");
		
		save.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				if (form.isValid()) {
				
					buttonBar.disable();
					
					final SampleInfo sampleInfo = sampleInfoModel.getBean();
					regSheetService.saveSampleInfo(sampleInfo, new AsyncCallback<Integer>() {
						public void onFailure(Throwable caught) {							
							
							buttonBar.enable();	
							MessageBox.alert("提示信息", "保存失败！", null);
						}

						public void onSuccess(Integer result) {							
							
							buttonBar.enable();								
							
							store.add(sampleInfoModel);
							store.getLoader().load();
							
							MessageBox.alert("提示信息", "保存成功！", null);
						}						
					});
				}
			}
		});

		buttonBar.add(save);
		buttonBar.add(reset);
	}


}
